<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@include file="/WEB-INF/templates/common/taglib.jsp"%>
<!DOCTYPE HTML>
<html>
  <head>
    <style>
      @media print {
      .paging{page-break-after:always}
      }
    </style>
     <script language="javascript" type="text/javascript" src="${basePath}/static/publics/assets/jquery/jquery.min.js"></script>
    <script type="text/javascript">
    var filelist = [ [
     /*  {src : "${basePath}/static/upload/file.png", loaded : false},
      {src : "${basePath}/static/upload/file.png", loaded : false},
      {src : "${basePath}/static/upload/file.png", loaded : false}, */
     /*  {src : "${basePath}/static/upload/file.png", loaded : false}, */
      {src : "${basePath}/static/upload/file.png", loaded : false}], [
      {src : "${basePath}/static/upload/clickshare.pdf", loaded : false}] ,
      [
       {src : "${basePath}/static/upload/clickshare.pdf", loaded : false} ]
      ];

    function printWindow() {
      var allOk = true;
      $("img.for_print").each(function(i, v) {
        allOk = allOk && v.complete;
        return allOk;
      });
      if(allOk) {
        console.log("all img load ok");
        var contentDiv = $("div#content").focus();
        window.print();
        setTimeout(function() { printPDF("${basePath}/static/upload/clickshare.pdf"); }, 100);
      }
      else {
        setTimeout(printWindow, 50);
        console.log("waiting all img load");
      }
    }

    function printPDF(url) {
      if (window.navigator.userAgent.indexOf("MSIE")>=1) {
    	  alert("我是IE");
        $("div#content").html("<iframe id=\"ifrm\" style=\"width:100%\" data=\"" + url + "\"></iframe>");
        var ifrm = $("#ifrm");
       
        
        ifrm.bind("readystatechange", function() {
          console.log("iframe state: " + this.readyState);
          if(this.readyState == 4) {
            console.log("pdf is ready, print " + this.src);
           // alert("IE");
            frames[0].focus();  
        frames[0].print(); 
            // setTimeout(PrintNextGroup...
          }else{
        	  alert("进来了吗?")
        	  frames[0].focus();  
              frames[0].print(); 
        	  
          }
        });
      }
      else {
        $("div#content").html("<iframe id=\"ifrm\" style=\"width:100%;display:none\" src=\"" + url + "\"></iframe>");
        var ifrm = $("#ifrm");
        alert(ifrm[0].contentWindow);
        console.log("google");
        ifrm[0].contentWindow.print();
        // setTimeout(PrintNextGroup...
      }
    }
    
    function imgCounter(imgList) {
      this.total = imgList.length;
      this.current = 0;
      this.increase = function() {
        current += 1;
        console.log(this.src);
        console.log(current);
        if(current == total) {
          setTimeout(printWindow, 50);
        }
      };
      return this;
    }

    function docReady() {
      console.log("doc ready");
      var ic = imgCounter(filelist[0]);
    
      var imglist ="";
 /*      filelist[0].forEach(function(v, i) {
        imglist += "<img class=\"for_print\" style=\"width:100%\" src=\"" + v.src + "\"></img><br class=\"paging\" />";
      }); */
      
      $.each( filelist[0],function(name,value){
  		imglist += "<img class=\"for_print\" style=\"width:100%\" src=\"" + this.src + "\"></img><br class=\"paging\" />";
  	}); 
      var contentDiv = $("div#content").html(imglist);
      $("img.for_print").attr("onload", ic.increase);
    }

    function allLoadOK() {
      console.log("all load OK");
    }

    $(document).ready(docReady);
    $(window).bind("load", allLoadOK);
    </script>
  </head>
  <body>
    <div id="content" style="width:1000px; display:block">
    </div>
  </body>
</html>
